package com.example.xm.controller;

import cn.hutool.core.date.DateUtil;
import com.example.xm.Entity.Dd;
import com.example.xm.config.PageResult;
import com.example.xm.servive.DdServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Dd")
public class DdController {
    @Autowired
    DdServive ddServive;

    /**显示订单信息*/
    @PostMapping("/spper")
    public List<Dd> Dd(@RequestBody Dd dd){
        System.out.println(ddServive.getAllDd(dd));
        return ddServive.getAllDd(dd);
    }

    /**分页*/
    @GetMapping("/pager")
    public PageResult selectPager(@RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum,
                                  @RequestParam(name = "pageSize",required = false,defaultValue = "5") int pageSize,
                                  @RequestParam(required = false,defaultValue = "")String dd_id,
                                  @RequestParam(required = false,defaultValue = "")String dd_hao,
                                  @RequestParam(required = false,defaultValue = "")String dd_date,
                                  @RequestParam(required = false,defaultValue = "")String dd_zf,
                                  @RequestParam(required = false,defaultValue = "")String dd_name,
                                  @RequestParam(required = false,defaultValue = "")String dd_ip,
                                  @RequestParam(required = false,defaultValue = "")String dd_xxip,
                                  @RequestParam(required = false,defaultValue = "")String dd_phone,
                                  @RequestParam(required = false,defaultValue = "")String dd_qq
    ){
        //获取信息
        Dd c = new Dd();
        c.setDd_id(dd_id);
        c.setDd_hao(dd_hao);
        c.setDd_zf(dd_zf);
        c.setDd_name(dd_name);
        c.setDd_ip(dd_ip);
        c.setDd_xxip(dd_xxip);
        c.setDd_phone(dd_phone);
        c.setDd_qq(dd_qq);
        if(!dd_date.equals("") && !dd_date.equals("null")){
            c.setDd_date(DateUtil.parse(dd_date,"yyyy-MM-dd HH:mm:ss"));
        }
        return ddServive.selectPager(pageNum,pageSize,c);
    }

    @PostMapping("")
    public int AddDdh(@RequestBody Dd dd){
        dd.setDd_date(new Date());
        System.out.println("添加了订单号：" + dd);
        return ddServive.AddDdh(dd);
    }
    /**修改*/
    @PutMapping("/dd")
    public int UpdateDd(@RequestBody Dd dd){
        System.out.println("修改了：" + dd);
        return ddServive.UpdateDd(dd);
    }

    @PostMapping("/add")
    public ResponseEntity<Object> addMultipleData(@RequestBody List<Dd> dataList) {
        try {
            ddServive.addMultipleData(dataList); // 调用服务层方法插入多个数据对象
            System.out.println(dataList);
            return ResponseEntity.ok("成功添加多条数据");
        } catch (Exception e) {
            System.out.println(dataList);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加数据失败");
        }

    }
}
